package com.getword.web.controller;


import com.getword.domain.Article;
import com.getword.domain.Reply;
import com.getword.domain.TbTest;
import com.getword.domain.Video;
import com.getword.mongodao.TbTestRepository;
import com.getword.mongodao.VideoRepository;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/test")
public class TestController {
    @Autowired
    private TbTestRepository dao;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private VideoRepository videoRepository;

    // 对于有ID的对象，执行保存操作就是执行更新操作，会造成字段消失
    @PostMapping
    public Object save(TbTest test){
        List<Reply> list = new ArrayList<>();
        Reply reply = new Reply();
        reply.setFrom(new HashMap<String, Object>(){{
            put("id",2);
            put("username","admin");
        }});
        reply.setFrom(new HashMap<String, Object>(){{
            put("id",3);
            put("username","admin");
        }});
        list.add(reply);
        test.setList(list);
        TbTest save = dao.save(test);
        Update update = new Update();
        return save;
    }

    public String update(TbTest test){
        return "";
    }


    // 复制字幕文件, 将指定视频的字幕文件复制给其他视频
    @GetMapping("/copyCaption")
    public String copyCaptions(String videoId){
        //
        Video video = videoRepository.findVideoById(videoId);
        mongoTemplate.updateMulti(Query.query(Criteria.where("captionText").is(null)),
                Update.update("captionText", video.getCaptionText()), Video.class);
        return "ok";
    }
    // 复制封面
    @GetMapping("/copyCover")
    public String copyCover(String videoId){
        //
        Video video = videoRepository.findVideoById(videoId);
        mongoTemplate.updateMulti(Query.query(Criteria.where("coverImg").is(null)),
                Update.update("coverImg", video.getCoverImg()), Video.class);
        return "ok";
    }

    // upload file
    @PostMapping("/upload")
    public String uploadFile(MultipartFile file) throws IOException {
        TbTest t = new TbTest();
        InputStream is = file.getInputStream();
        String text = IOUtils.toString(is, StandardCharsets.UTF_8);
        t.setText(text);
        dao.save(t);
        return "ok";
    }
    @GetMapping("/readFile")
    public String readFile(String id){
        TbTest t = dao.findTbTestById(id);
        byte[] bs = t.getBs();

        return "ok";
    }

    // 给文章添加假的keywords字段
    @GetMapping("/keyword")
    public String keyword(){
        List<String> keywords = new ArrayList<>();
        keywords.add("person");
        keywords.add("我们");
        keywords.add("学校");
        mongoTemplate.updateMulti(Query.query(Criteria.where("title").regex(".*")), Update.update("keywords", keywords), Article.class);
        return "ok";
    }

    @PostMapping("/articleSummary")
    public String summary(String name){
        mongoTemplate.updateMulti(Query.query(Criteria.where("title").regex(".*")),
                Update.update("summary", name), Article.class);
        return "ok";
    }
}
